import blobToFile from "../convert/blob-to-file.js";
import formatContentDisposition from "../format/format-content-disposition.js";

/**
 * @param { string } url
 */
export default async function readFile(url) {
    const response = await fetch(url, { method: "get" });
    if (!response.ok) {
        throw new Error(response.statusText);
    }
    const contentDisposition = response.headers.get("Content-Disposition");
    const fileInfo = formatContentDisposition(contentDisposition);
    const blob = await response.blob();
    const filename = fileInfo.filename || new Date().getTime().toString();
    return blobToFile(filename, blob);
}
